#include <stdio.h>
#include <stdlib.h>
#include "kernel_list.h"


#define log(fmt, arg...) printf(""fmt"\n",##arg)

typedef enum {
    IDLE_LIST,
    WIP_LIST
} e_worker_type_t;

typedef struct {
    unsigned int value;
    struct list_head head;
} hdata_t;

int main(int argc, char **argv) {
    struct list_head workers[2];

    hdata_t *data = NULL, *tmp = NULL;

    INIT_LIST_HEAD(&workers[IDLE_LIST]);
    INIT_LIST_HEAD(&workers[WIP_LIST]);


    for (int i = 1; i <= 10; i++) {
        data = (hdata_t *)malloc(sizeof(hdata_t));

        data->value = i * 5;
        list_add_tail(&data->head, &workers[IDLE_LIST]);
        log("[ INF ]init data: %p(%d)", data, data->value);
    }

    if (list_empty_careful(&workers[IDLE_LIST])) {
        log("[ ERR ] empty idle list.");
    } else {
        list_for_each_entry_safe(data, tmp, &workers[IDLE_LIST], head) {
            if (data->value >= 30) {
                log("[ INF ] Found data: %p(%d), move to wip...", data, data->value);
                list_del(&data->head);
                list_add_tail(&data->head, &workers[WIP_LIST]);
            }
        }
    }

    list_for_each_entry_safe(data, tmp, &workers[WIP_LIST], head) {
        list_del(&data->head);
        log("[ INF ] WIP: remove data: %p(%d)", data, data->value);
        free(data);
        data = NULL;
    }

    list_for_each_entry_safe(data, tmp, &workers[IDLE_LIST], head) {
        list_del(&data->head);
        log("[ INF ] IDLE: remove data: %p(%d)", data, data->value);
        free(data);
        data = NULL;
    }

    return 0;
}
